package com.wva.video.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wva.common.context.UserContext;
import com.wva.video.domain.dto.VideoInteractionLogDTO;
import com.wva.video.domain.po.VideoInteraction;
import com.wva.video.enums.InteractionType;
import com.wva.video.enums.LikeOperation;
import com.wva.video.enums.QueueEnum;
import com.wva.video.enums.RedisKey;
import com.wva.video.mapper.VideoInteractionMapper;
import com.wva.video.service.VideoInteractionService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hong
 * @since 2025-03-09
 */
@Service
public class VideoInteractionServiceImpl extends ServiceImpl<VideoInteractionMapper, VideoInteraction> implements VideoInteractionService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    RabbitTemplate rabbitTemplate;

    @Override
    public boolean likeVideo(Long videoId) {
        String key = RedisKey.LIKE_SET.format(videoId);
        Long res = stringRedisTemplate.opsForSet().add(key, String.valueOf(UserContext.getUserId()));
        //发送到mq进行持久化
        sendToPersistenceQueue(videoId, InteractionType.LIKE, builder -> {
            builder.likeOperation(LikeOperation.LIKE);
        });
        return Long.valueOf(1).equals(res);
    }

    @Override
    public boolean unlikeVideo(Long videoId) {
        String key = RedisKey.LIKE_SET.format(videoId);
        Long res = stringRedisTemplate.opsForSet().remove(key, String.valueOf(UserContext.getUserId()));
        //发送到mq进行持久化
        sendToPersistenceQueue(videoId, InteractionType.LIKE, builder -> {
            builder.likeOperation(LikeOperation.UNLIKE);
        });
        return Long.valueOf(1).equals(res);
    }

    @Override
    public boolean isLikeVideoByVideoId(Long videoId) {
        String key = RedisKey.LIKE_SET.format(videoId);
        Boolean like = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(UserContext.getUserId()));
        return Boolean.TRUE.equals(like);
    }

    @Override
    public Long getVideoLikeCountByVideoId(Long videoId) {
        String key = RedisKey.LIKE_SET.format(videoId);
        return Optional.ofNullable(stringRedisTemplate.opsForSet().size(key)).orElse(0L);
    }

    private void sendToPersistenceQueue(Long videoId, InteractionType interactionType, Consumer<VideoInteractionLogDTO.VideoInteractionLogDTOBuilder> useBuilder) {
        //默认构造参数
        VideoInteractionLogDTO.VideoInteractionLogDTOBuilder builder = VideoInteractionLogDTO.builder()
                .interactionType(interactionType)
                .videoId(videoId)
                .userId(UserContext.getUserId());
        //自定义构造参数
        useBuilder.accept(builder);

        rabbitTemplate.convertAndSend(
                QueueEnum.INTERACTION_LOG_1.getExchange(),
                QueueEnum.INTERACTION_LOG_1.getRoutingKey(),
                builder.build()
        );
    }
}
